<template>
  <el-dialog title="提示" :visible.sync="editRoleDialog" width="50%" @close="editClose">
    <el-form
      :model="userinfo"
      :rules="editRoleRules"
      ref="editRoleRef"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="活动名称" prop="roleName">
        <el-input v-model="userinfo.roleName"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="roleDesc">
        <el-input v-model="userinfo.roleDesc"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="editRoleDialog = false">取 消</el-button>
      <el-button type="primary" @click="editRoleReq">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: {
    visible: {
      type: Boolean
    },
    userinfo: {
      type: Object
    }
  },
  data() {
    return {
      editRoleDialog: false,
      editRoleRules: {
        roleName: [
          { required: true, message: '请输入角色名', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2~10位角色名', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2~10位角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 提交修改
    editRoleReq() {
      this.$refs.editRoleRef.validate(async valid => {
        if (!valid) return false
        const { data: res } = await this.$http.put(
          `roles/${this.userinfo.id}`,
          {
            roleName: this.userinfo.roleName,
            roleDesc: this.userinfo.roleDesc
          }
        )
        console.log(res)

        if (res.meta.status !== 200) {
          this.$emit('editRoleSuccess')
          return this.$message.error('更新数据失败')
        }
        this.$message.success('更新成功')
        this.$emit('editRoleSuccess')
        this.editRoleDialog = false
      })
    },
    editClose() {
      this.$refs.editRoleRef.resetFields()
    }
  },
  watch: {
    visible: function(val) {
      this.editRoleDialog = val
    },
    editRoleDialog: function(val) {
      this.$emit('update:visible', val)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
